<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>虚拟订单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>虚拟订单{{detail.id?'修改':'添加'}}</legend>
        </fieldset>

        <form class="layui-form" action="javascript:;" lay-filter="this-form">
            <input type="hidden" name="id" :value="detail.id">

            <div class="layui-form-item">
                <label class="layui-form-label">服务器:</label>
                <div class="layui-input-block">
                    <select name="server_id" class="layui-select" >
                        <option value="">--选择区服--</option>
                        <option v-for="(item,index) in services" :value="item.server_id" lay-ignore="">{{item.name}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">长ID:</label>
                <div class="layui-input-block">
                    <input type="number" step="1" name="customer_id" :value="detail.customer_id" autocomplete="off"
                           v-on:blur="customerIdChange"
                           placeholder="请输入长ID" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">游戏角色:</label>
                <div class="layui-input-block">
                    <select name="role_id" class="layui-select" lay-search>
                        <option value="">选择游戏角色</option>
                        <option v-for="item in roles" :value="item.id" lay-ignore="">{{item.nick}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">渠道:</label>
                <div class="layui-input-block">
                    <select class="layui-select"  lay-filter="setGemValue">
                        <option v-for="option in channelOptions" :key="option.type" :value="option.type">{{option.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内购商品:</label>
                <div class="layui-input-block">
                    <select name="goods_id" class="layui-select"  lay-search>
                        <option value="">选择内购商品</option>
                        <option v-for="item in gameGoodsList" :value="item.id" lay-ignore="">{{item.name}} - {{item.detail}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">数量:</label>
                <div class="layui-input-block">
                    <input type="number" step="1" name="number" :value="detail.number" autocomplete="off" placeholder="请输入数量" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注:</label>
                <div class="layui-input-block">
                    <input type="text" name="comment" :value="detail.comment" autocomplete="off" placeholder="请输入备注" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>

                </div>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    var Id = urlParam('id')
    var form = null;
    var laydate = null;
    var _this=null;
    var $ = null;
    var layer = null;
    var channels = getEnum('configgroup')


    new Vue({
        el:"#app",
        data:{
            detail:{ // Vue 赋值, layui取值
                id:Id,
                customer_id:null,
                goods_id:null,
                server_id:null,
                role_id:null,
                number:null,
                comment:null,
            },

            selectedChannel: null,
            channelOptions: channels,
            gameGoodsList:[],
            services: JSON.parse(localStorage.getItem('services')),
            roles:[],
        },
        methods:{
            customerIdChange(e){
                let customerId = e.target.value;
                let formData = form.val('this-form');
                let gameArea = formData.server_id;
                var index = layer.load();
                _this.detail.customer_id = customerId;
                $.get('/admin/gameRole/paginate',{gameArea:gameArea,customerId:customerId,fields:['id','nick']},function (res) {
                    layer.close(index)
                    if (res.code===0){
                        _this.roles = res.data
                    } else {
                        _this.roles = []
                    }
                })
            },


        },
        created:function(){
            _this = this;
            layui.use(['form','laydate'],function(){
                $ = layui.$;
                form = layui.form;
                laydate = layui.laydate;
                layer = layui.layer;
            })

        },
        updated:function(){
            form.render();
        },
        mounted:function(){
            layui.use(['form'], function () {

                //初始化
            if (Id){ //编辑操作
                $.get("/admin/virtualOrder/"+Id,{},function(res){ //获取数据
                    _this.detail = res.data
                    if (res.code===0){
                        _this.detail = res.data;
                        form.val('this-form',res.data)
                    }
                });
            }
            //默认选择官服商品列表
                 $.get('/admin/gameGoods/paginate',{limit:9999999,channeltype:0},function (res) {
                    _this.gameGoodsList = res.data;
                  })
                form.on('select(setGemValue)',function(data){ //宝石
                    console.log('我选择了某个服务器',data)
                    var selectId = data.value;
                    _this.selectedChannel = data.value;

                    $.get('/admin/gameGoods/paginate',{limit:9999999,channeltype:selectId},function (res) {
                            _this.gameGoodsList = res.data;
                         })
                })
           // $.get('/admin/gameGoods/paginate',{limit:9999999},function (res) {
            //    _this.gameGoodsList = res.data;
          //  })
            //监听提交
            form.on('submit(submit)', function (data) {
                var formData = data.field;
                formData.selectedType=_this.selectedChannel;

                if (Id){ //update
                    formData._method = 'put';
                    $.post("/admin/virtualOrder/"+Id,formData,function(res){
                        if (res.code===0){
                            layer.msg('success',{icon:6,time:500},function(){
                                parent.location.reload();//父页面刷新
                            })
                        } else {
                            layer.msg(res.msg,{icon:5})
                        }
                    })
                } else { //add
                    $.post("/admin/virtualOrder/insert",formData,function(res){
                        if (res.code===0){
                            layer.msg('success',{icon:6,time:500},function(){
                                parent.location.reload(); //父页面刷新
                            })
                        } else {
                            layer.msg(res.msg,{icon:5})
                        }
                    })
                }
                return false;
            });
        });
    }
    })
</script>
</body>
</html>
